package com.scsj.supermarket.view.activity.adressmodel;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.scsj.supermarket.R;
import com.scsj.supermarket.base.AddAndAlterDeliveryAddressBase;
import com.scsj.supermarket.bean.BaseBean;
import com.scsj.supermarket.bean.DeliveryAddressBean;
import com.scsj.supermarket.presenter.AddAndAlterDeliveryAddressPresenter;
import com.scsj.supermarket.utils.DialogChangeAdress;
import com.scsj.supermarket.utils.ToastMiddleUtils;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;
import com.scsj.supermarket.view.activity.mapmodel.AddShopMarkActivity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.scsj.supermarket.view.activity.shopermodel.RegisterFirstStepActivity.GOMAPCODE;

/**
 * 收货地址编辑
 */
public class AdressEditActivity extends BaseActivity implements View.OnClickListener, AddAndAlterDeliveryAddressBase.IView {

    //标签
    private RecyclerView adress_edit_rv;
    private List<String> data;
    private TextView city_choice_tv;
    //联系人
    private ImageView adress_edit_contacts_img;
    private EditText adress_edit_user_name;
    private EditText adress_edit_user_phone;
    private static final int PICK_CONTACT = 0;

    private TextView family_tv, company_tv, school_tv, increase_tv;
    private String newlable = "";
    private LinearLayout add_lable_ll, new_lable_ll;
    private EditText edit_add_lable_et;//输入新标签
    private TextView add_confirm_tv;//确认增加标签
    private TextView fresh_lable_tv;//新增好的标签
    private TextView edit_fresh_confirm_tv;//编辑

    private TextView adress_edit_preservation;

    private TextView cancel_tv, confirm_tv;
    private Dialog dialog1;
    private DeliveryAddressBean.DataBean dataBean;
    private AddAndAlterDeliveryAddressPresenter addAndAlterDeliveryAddressPresenter ;


    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_adress_edit);
        addAndAlterDeliveryAddressPresenter = new AddAndAlterDeliveryAddressPresenter(this);
        dataBean = (DeliveryAddressBean.DataBean) getIntent().getSerializableExtra("adress");

    }

    /**
     * 初始化View
     */
    private void InitializationView() {
        edit_add_lable_et = findViewById(R.id.edit_add_lable_et);
        add_confirm_tv = findViewById(R.id.add_confirm_tv);
        fresh_lable_tv = findViewById(R.id.fresh_lable_tv);
        edit_fresh_confirm_tv = findViewById(R.id.edit_fresh_confirm_tv);
        add_lable_ll = findViewById(R.id.add_lable_ll);
        new_lable_ll = findViewById(R.id.new_lable_ll);
        family_tv = findViewById(R.id.family_tv);
        company_tv = findViewById(R.id.company_tv);
        school_tv = findViewById(R.id.school_tv);
        increase_tv = findViewById(R.id.increase_tv);
        adress_edit_rv = findViewById(R.id.adress_edit_rv);
        city_choice_tv = findViewById(R.id.city_choice_tv);
        adress_edit_contacts_img = findViewById(R.id.adress_edit_contacts_img);
        adress_edit_user_name = findViewById(R.id.adress_edit_user_name);
        adress_edit_user_phone = findViewById(R.id.adress_edit_user_phone);
        adress_edit_preservation = findViewById(R.id.adress_edit_preservation);
        adress_edit_preservation.setOnClickListener(this);
        family_tv.setOnClickListener(this);
        company_tv.setOnClickListener(this);
        school_tv.setOnClickListener(this);
        increase_tv.setOnClickListener(this);
        add_confirm_tv.setOnClickListener(this);
        fresh_lable_tv.setOnClickListener(this);
        edit_fresh_confirm_tv.setOnClickListener(this);
        city_choice_tv.setOnClickListener(this);
        adress_edit_contacts_img.setOnClickListener(this);
    }

    private void InitializationLable() {
        family_tv.setBackgroundResource(R.drawable.lable_text_side_line_unselect);
        company_tv.setBackgroundResource(R.drawable.lable_text_side_line_unselect);
        school_tv.setBackgroundResource(R.drawable.lable_text_side_line_unselect);
        fresh_lable_tv.setBackgroundResource(R.drawable.lable_text_side_line_unselect);
//        increase_tv.setBackgroundResource(R.drawable.lable_text_side_line_unselect);


        /**
         * 编辑地址的话把传过来地址赋值
         */
        if(dataBean != null) {
            Logger.d("dataBean==>" + dataBean.toString());
            adress_edit_user_name.setText(dataBean.getUserDeliveryAddress().getReceiverName());
            adress_edit_user_phone.setText(dataBean.getUserDeliveryAddress().getReceiverPhone());
            city_choice_tv.setText(dataBean.getLocation().getAddress());

        }
    }

    private void Label() {
        data = new ArrayList<>();
        data.add("家");
        data.add("公司");
        data.add("学校");
//        adressEditAdapter = new AdressEditAdapter(data);
//        adress_edit_rv.setAdapter(adressEditAdapter);

    }


    /**
     * 获取联系人信息
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case (PICK_CONTACT):
                if (resultCode == Activity.RESULT_OK) {
                    Uri contactData = data.getData();
                    Cursor c = managedQuery(contactData, null, null, null, null);
                    if (c.moveToFirst()) {
                        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                        String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                        String phoneNumber = null;
                        if (hasPhone.equalsIgnoreCase("1")) {
                            hasPhone = "true";
                        } else {
                            hasPhone = "false";
                        }
                        if (Boolean.parseBoolean(hasPhone)) {
                            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
                                            + contactId,
                                    null,
                                    null);
                            while (phones.moveToNext()) {
                                phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            }
                            phones.close();
                        }
                        adress_edit_user_name.setText(name);
                        adress_edit_user_phone.setText(phoneNumber);
                    }
                }
                break;

                case GOMAPCODE://进入地图之后处理数据
                    if (data.getStringExtra("detail_address") != null) {
                        String detailAddress = data.getStringExtra("detail_address");
                        city_choice_tv.setText(detailAddress);
                    }
                    break;
        }
    }

    /**
     * 跳转系统联系人
     */
    private void GetAContact() {
        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent, PICK_CONTACT);
    }

    private void showAuthenticationStateDialog1() {
        View view = getLayoutInflater().inflate(R.layout.dialog_bank_cards_particulars, null, false);
        TextView hint_tv = view.findViewById(R.id.hint_tv);
        hint_tv.setText("修改的信息还未保存，确认现在返回\n么？");
        cancel_tv = view.findViewById(R.id.cancel_tv);
        confirm_tv = view.findViewById(R.id.confirm_tv);
        cancel_tv.setOnClickListener(this);
        confirm_tv.setOnClickListener(this);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view);
        // 创建对话框
        dialog1 = builder.create();
        // 显示对话框
        dialog1.show();

    }

    @Override
    protected void initView() {
        InitializationView();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.returnImg:
                showAuthenticationStateDialog1();
                break;
            case R.id.city_choice_tv:
               // DialogChangeAdress.showAuthenticationStateDialog(this);
                //进入百度地图选择地址界面
                Intent intent = new Intent(mContext, AddShopMarkActivity.class);
                intent.putExtra("isAddressEdit","isAddressEdit");
                startActivityForResult(intent, GOMAPCODE);
                break;
            case R.id.adress_edit_contacts_img:
                GetAContact();
                break;
            case R.id.adress_edit_preservation:
                // TODO: 2018/7/2  联网
                if(dataBean == null ){//新增地址

//                    addAndAlterDeliveryAddressPresenter.deliverAddDAData();
                }else {//修改地址

//                    addAndAlterDeliveryAddressPresenter.deliverAlterDAData();
                }
                finish();
                break;
            case R.id.family_tv:
                InitializationLable();
                family_tv.setBackgroundResource(R.drawable.lable_text_side_line_select);
                break;
            case R.id.company_tv:
                InitializationLable();
                company_tv.setBackgroundResource(R.drawable.lable_text_side_line_select);
                break;
            case R.id.school_tv:
                InitializationLable();
                school_tv.setBackgroundResource(R.drawable.lable_text_side_line_select);
                break;
            case R.id.increase_tv:
                increase_tv.setVisibility(View.GONE);
                add_lable_ll.setVisibility(View.VISIBLE);
                break;
            case R.id.add_confirm_tv://确认增加标签
                if (edit_add_lable_et.getText().length() > 5) {
                    ToastMiddleUtils.ToastShow(this, null, "最多只能输入5个字哦~");
                } else {
                    add_lable_ll.setVisibility(View.GONE);
                    new_lable_ll.setVisibility(View.VISIBLE);
                    fresh_lable_tv.setText(edit_add_lable_et.getText());
                }
                break;
            case R.id.fresh_lable_tv://新增好的标签
                InitializationLable();
                fresh_lable_tv.setBackgroundResource(R.drawable.lable_text_side_line_select);
                break;
            case R.id.edit_fresh_confirm_tv://编辑新增的标签
                add_lable_ll.setVisibility(View.VISIBLE);
                new_lable_ll.setVisibility(View.GONE);
                break;
            case R.id.cancel_tv:
                dialog1.dismiss();
                break;
            case R.id.confirm_tv:
                dialog1.dismiss();
                finish();
                break;

        }
    }

    @Override
    public void AddDASuccessMsg(String s, BaseBean addDABean) {

    }

    @Override
    public void AlterDASuccessMsg(String s, BaseBean deleteDABean) {

    }

    @Override
    public void showDAErrorMsg(String s) {

    }


}
